package com.it.bannner

import android.app.Activity
import android.app.Application
import android.os.Bundle
import com.haiheng.voiceandbook.Constants.Companion.ROOT_PATH
import com.it.bannner.log.LogWriter
import com.tencent.bugly.crashreport.CrashReport
import java.io.File
import java.lang.Exception
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit


class MyApplication : Application() {
    val TAG = javaClass.simpleName
    override fun onCreate() {
        super.onCreate()
        instance = this
        LogWriter.init(TAG, true,true)
        //初始化 bugly 崩溃收集  13129554264
        //初始化 bugly 崩溃收集  13129554264
        CrashReport.initCrashReport(applicationContext, "cd77e90157", false)
        initFileDir()
        initThreadPool()
//        val  apkPath = Environment.getExternalStorageDirectory().absolutePath +"/a.apk"
//        val m = XHApiManager()
//        Handler().postDelayed({
//            LogWriter.e( "5s之后安装")
//            m.XHInstallOnBackground(apkPath,"com.it.bannner")
//            LogWriter.e( "安装完成")
//        },5000)
        System.setProperty("http.keepAlive", "false");
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                MyActivityManager.getInstance().addActivity(activity)
            }

            override fun onActivityStarted(activity: Activity) {}
            override fun onActivityResumed(activity: Activity) {
                MyActivityManager.getInstance().currentActivity = activity
            }

            override fun onActivityPaused(activity: Activity) {}
            override fun onActivityStopped(activity: Activity) {}
            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
            override fun onActivityDestroyed(activity: Activity) {
                MyActivityManager.getInstance().removeActivity(activity)
            }
        })



    }

    /**
     * 线程池
     */
    lateinit var executor: ThreadPoolExecutor

    lateinit var executorUpdate: ThreadPoolExecutor

    /**
     *  初始化线程池
     */
    private fun initThreadPool() {
        /**
         * 阻塞队列
         */
        var queue = LinkedBlockingQueue<Runnable>()
        /**
         * 线程池
         */
        executor = ThreadPoolExecutor(1, 1, 30, TimeUnit.MINUTES, queue)

        /**
         * 线程池
         */
        executorUpdate = ThreadPoolExecutor(1, 1, 30, TimeUnit.MINUTES, queue)
    }

    /**
     * 初始化下载的文件夹
     */
    private fun initFileDir() {
        val fileDr = File(ROOT_PATH)
        if (!fileDr.exists()) {
            fileDr.mkdir()
        }
    }

    //伴生对象
    companion object {
        private lateinit var instance: MyApplication
        fun getInstance(): MyApplication {
            return instance
        }
    }


}